package com.itheima.algorithm.hashtable;

import java.util.HashMap;

/**
 * @author: TylerZhong
 * @description: 无重复字符的最长子串
 * 1. 给定一个字符串 s, 请你找出其中不含有重复字符的最长子串的长度
 * 2. s 由英文字母、数字、符号和空格组成
 */
public class E02Leetcode3 {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> hash = new HashMap();
        int begin = 0;
        int maxLength = 0;
        for (int end = 0; end < s.length(); end++) {
            char ch = s.charAt(end);
            if (hash.containsKey(ch)) { // 重复时调整 begin
                begin = Math.max(begin, hash.get(ch) + 1);
            }
            hash.put(ch, end);
//            System.out.println(s.substring(begin, end+1));
            maxLength = Math.max(maxLength, end - begin + 1);
        }
        return maxLength;
    }

    public static void main(String[] args) {
//        String s = "abcabcbb";
        String s = "abba";
        System.out.println(new E02Leetcode3().lengthOfLongestSubstring(s));
    }

}
